package com.xujiang.imitationcompanypj;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.etekcity.vesync.common.*;
import com.etekcity.vesync.common.BuildConfig;
import com.etekcity.vesync.common.plus.util.OkHttp3Utils;
import com.etekcity.vesync.common.util.Callback;
import com.etekcity.vesync.common.util.L;
import com.etekcity.vesync.common.util.NetException;
import com.etekcity.vesync.common.util.NetLocalException;
import com.etekcity.vesync.common.util.NetServerException;
import com.etekcity.vesync.common.util.PreferencesUtils;
import com.etekcity.vesync.common.util.SystemRuntimeException;
import com.fuck_boilerplate.rx_paparazzo.RxPaparazzo;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.xujiang.imitationcompanypj.data.Data;
import com.xujiang.imitationcompanypj.data.model.ServerError;
import com.xujiang.imitationcompanypj.model.UserLogin;
import com.xujiang.imitationcompanypj.util.Constants;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by xujiang on 2017/3/31.
 */

public class EApplication extends MultiDexApplication{
    private final  String TAG = EApplication.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Common.init(this, Constants.PROJECT_NAME, BuildConfig.DEBUG);
        RxPaparazzo.register(this); //rxjava扩展Android采取图像使用相机和画廊
        Data.init(getApplicationContext(),Constants.BASE_HTTP_SERVER_ADDRESS,
            OkHttp3Utils.init().setErrorHandleListener(new OkHttp3Utils.ErrorHandleListener(){

                @Override
                public Response checkError(Response response) throws NetException {

                    if(!response.isSuccessful()) {

                        throw new SystemRuntimeException(new NetLocalException(
                                //getApplicationContext().getString(R.string.ERR_NETWORE)
                                "Network Error", response.code()));
                    }
                    String error = response.header("error");
                    if(!TextUtils.isEmpty(error)){
                             String errString = ServerError.getErrString(getApplicationContext(),Integer.parseInt(error));
                            throw  new SystemRuntimeException(new NetServerException(errString,new ServerError(error,errString)));
                    }
                    return  response;
                    }
                }).addHeaderPlus(
                    new OkHttp3Utils.HeaderPlusListener(){

                        @Override
                        public Request.Builder headerInterceptor(Request originalRequest, Request.Builder builder, Map<String, Object> parameters) {
                            if(UserLogin.get().isLogin()){
                                builder.addHeader("uniqueId", UserLogin.get().getUserId());
                                builder.addHeader("tk", UserLogin.get().getAccessToken());
                            }
                            return  builder;
                        }
                    }).setBuilder(new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60,TimeUnit.SECONDS).readTimeout(60,TimeUnit.SECONDS))
        );
        PreferencesUtils.init(this);
//        APRetrofitUtils.get().init(APConstants.BASE_HTTP_SERVER_ADDRESS);
//        HumidRetrofitUtils.get().init(HumidConstant.BASE_HTTP_SERVER_ADDRESS);
//        NSRetrofitUtils.get().init(NSConstants.BASE_HTTP_SERVER_ADDRESS);
//        WSRetrofitUtils.get().init(WSConstants.BASE_HTTP_SERVER_ADDRESS);
        UserLogin.get().readLoginInfo();

//        if (BuildConfig.DEBUG) {
//            PushAgent agent = PushAgent.getInstance(this);
//            agent.setDebugMode(false);
//            agent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE); //声音
//            agent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);//呼吸灯
//            agent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);//振动
//            agent.setNotificationClickHandler(new VesyncUmengNotificationClickHandler());
//            agent.register(new IUmengRegisterCallback() {
//                @Override public void onSuccess(String s) {
//                    if (UserLogin.get().isLogin()) {
//                        NetworkClient.get()
//                                .setUserPushToken(s, new Callback.LogEmptyCallback<ResponseBody>());
//                    }
//                    L.i(TAG, "Umeng onRegistered id: %s", s);
//                }
//
//                @Override public void onFailure(String s, String s1) {
//                }
//            });
//        }

    }
}
